﻿<navigation:Page x:Class="SampleBrowser.Views.SampleWarnings" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:doc="http://schemas.firstfloorsoftware.com/documenttoolkit"
           xmlns:dte="http://schemas.firstfloorsoftware.com/documenttoolkit/extensions"
           xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
           xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Warnings">
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="136" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <ControlTemplate x:Key="WarningsFixedPageViewerTemplate" TargetType="doc:FixedPageViewer">
                <Grid x:Name="LayoutRoot">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="PageStates">
                            <VisualState x:Name="PageLoading" />
                            <VisualState x:Name="PageLoaded">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageContainer" Storyboard.TargetProperty="Visibility" Duration="0">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PageError">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageErrorElement" Storyboard.TargetProperty="Visibility" Duration="0">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid x:Name="PageContainer"  Visibility="Collapsed" >
                        <ContentPresenter x:Name="PagePresenter" Content="{TemplateBinding FixedPage}"/>

                        <!-- Visualize Warnings-->
                        <Grid Width="{Binding FixedPageSize.Width, RelativeSource={RelativeSource TemplatedParent}}">
                            <Border Margin="16" Padding="8" Background="LightYellow" BorderBrush="Silver" BorderThickness="1" CornerRadius="8" VerticalAlignment="Top" >
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <TextBlock FontWeight="Bold">
                                    <Run Text="{Binding FixedPage.Warnings.Count, RelativeSource={RelativeSource TemplatedParent}, FallbackValue=0}" />
                                    <Run Text="Warning(s)" />
                                    </TextBlock>
                                    <ItemsControl Grid.Row="1" ItemsSource="{Binding FixedPage.Warnings, RelativeSource={RelativeSource TemplatedParent}}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock TextWrapping="Wrap">
                                                <Run Text="XPS" /><Run Text="{Binding Number}" /><Run Text=":" />
                                                <Run Text="{Binding Text}" /><Run Text=", Level:" />
                                                <Run Text="{Binding Level}" /><Run Text=", Source:" />
                                                <Run Text="{Binding Source}" />
                                                </TextBlock>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Grid>
                            </Border>
                        </Grid>
                    </Grid> 
                    <Grid x:Name="PageErrorElement" Visibility="Collapsed" Width="{Binding FixedPageSize.Width, RelativeSource={RelativeSource TemplatedParent}}" Height="{Binding FixedPageSize.Height, RelativeSource={RelativeSource TemplatedParent}}">
                        <!-- Provides a formatted error page -->
                        <RichTextBox IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap" Margin="32">
                            <RichTextBox.DataContext>
                                <doc:VersionInfo />
                            </RichTextBox.DataContext>
                            <Paragraph>
                                <Run FontSize="32" Foreground="Red" Text="Failed to Render Page" />
                                <LineBreak />
                                <LineBreak />
                                <InlineUIContainer>
                                    <Line X2="1" Stretch="Fill" StrokeThickness="1" Stroke="#CCCCCC" />
                                </InlineUIContainer>
                                <LineBreak />
                                <LineBreak />
                                <Run FontSize="20" FontStyle="Italic" Foreground="Maroon" Text="{Binding FixedPageError.Message, RelativeSource={RelativeSource TemplatedParent}}" />
                                <LineBreak />
                                <LineBreak />
                                <Run FontSize="12" FontWeight="Bold" Text="Description:" />
                                <Run FontSize="12" Text="An exception occurred during the rendering of this page. Please review the stacktrace for more information about the error." />
                                <LineBreak />
                                <LineBreak />
                                <Run FontSize="12" FontWeight="Bold" Text="Exception Details:" />
                                <LineBreak />
                                <Run FontSize="12" Text="{Binding FixedPageError, RelativeSource={RelativeSource TemplatedParent}}" />
                                <LineBreak />
                                <LineBreak />
                                <InlineUIContainer>
                                    <Line X2="1" Stretch="Fill" StrokeThickness="1" Stroke="#CCCCCC" />
                                </InlineUIContainer>
                                <LineBreak />
                                <LineBreak />
                                <Run FontSize="12" FontWeight="Bold" Text="Version Information:" />
                                <Run FontSize="12" Text="{Binding DisplayName}" />
                            </Paragraph>
                        </RichTextBox>
                    </Grid>
                </Grid>
            </ControlTemplate>
            <ControlTemplate x:Key="WarningsDocumentViewerTemplate" TargetType="doc:DocumentViewer">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ScrollViewer x:Name="ScrollViewer" Background="Transparent" BorderThickness="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                        <ItemsControl x:Name="Pages">
                            <i:Interaction.Behaviors>
                                <doc:PageVirtualizer />
                            </i:Interaction.Behaviors>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid Width="{Binding DesiredPageContainerSize.Width}"
                                          Height="{Binding DesiredPageContainerSize.Height}">
                                        <Border Background="White" BorderBrush="{StaticResource SliderTrackBrush}" BorderThickness="1" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Center">
                                            <doc:FixedPageViewer FixedPage="{Binding FixedPage}" 
                                                                 FixedPageSize="{Binding FixedPageSize}"
                                                                 FixedPageError="{Binding FixedPageError}"
                                                                 Scale="{Binding DesiredPageScale}"
                                                                 Template="{StaticResource WarningsFixedPageViewerTemplate}">
                                                <i:Interaction.Behaviors>
                                                    <doc:NavigationBehavior TextContainer="{Binding TextContainer}" />
                                                </i:Interaction.Behaviors>
                                            </doc:FixedPageViewer>
                                        </Border>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Grid.Resources>

        <TextBlock x:Name="HeaderText" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" Text="Warnings"/>

        <doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError">
            <doc:DocumentDataSource.LoadPageSettings>
                <doc:LoadFixedPageSettings TreatWarningsAsErrors="False" WarningLevel="All" />
            </doc:DocumentDataSource.LoadPageSettings>
        </doc:DocumentDataSource>

        <doc:ThumbnailListBox x:Name="Thumbnails" Grid.Row="1" DocumentDataSource="{Binding ElementName=DataSource}" PageIndex="{Binding PageIndex, Mode=TwoWay, ElementName=Navigator}" />

        <doc:DocumentViewer x:Name="Viewer"
                            Template="{StaticResource WarningsDocumentViewerTemplate}"
                            Grid.Column="1" Grid.Row="1"
                            DocumentDataSource="{Binding ElementName=DataSource}"
                            ViewMode="{Binding SelectedViewMode, ElementName=ViewModePicker}">
        </doc:DocumentViewer>

        <StackPanel Grid.ColumnSpan="2" Grid.Row="2" Orientation="Horizontal" Margin="0,8,0,0" HorizontalAlignment="Center">
            <doc:PageNavigator x:Name="Navigator"
                               PageCount="{Binding PageCount, ElementName=Viewer}"
                               PageIndex="{Binding PageIndex, ElementName=Viewer, Mode=TwoWay}"
                               Margin="4,0,0,0" />
            <doc:ViewModePicker x:Name="ViewModePicker" Margin="4,0,0,0" />

            <Button Margin="4,0,0,0" Padding="7">
                <Button.Command>
                    <dte:OpenPdfOrXpsFromDiskCommand DocumentDataSource="{Binding ElementName=DataSource}" />
                </Button.Command>
                <Button.Content>
                    <Image Source="/SampleBrowser;component/Assets/Icons/open.png" />
                </Button.Content>
            </Button>
            <TextBlock Text="Warning Level:" Margin="8,0,0,0" VerticalAlignment="Center" />
            <ComboBox x:Name="ComboWarningLevel" SelectedValue="{Binding LoadPageSettings.WarningLevel, ElementName=DataSource, Mode=TwoWay}" Margin="4,0,0,0" />
            <CheckBox Content="Treat Warnings As Errors" IsChecked="{Binding LoadPageSettings.TreatWarningsAsErrors, ElementName=DataSource, Mode=TwoWay}" Margin="8,0,0,0" VerticalAlignment="Center"/>

        </StackPanel>
    </Grid>
</navigation:Page>
